package com.klxedu.ms.gateway.security.authentication.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import com.klxedu.ms.gateway.core.json.BaseJsonObject;
import com.klxedu.ms.gateway.security.client.UserFeignClient;
import com.klxedu.ms.gateway.security.client.service.LoginUser;
import com.klxedu.ms.gateway.security.config.AutoLoginPro;
import com.klxedu.ms.gateway.utils.SpringBeanUtils;

public class CsLoginidAuthenticateAction extends AbstraAuthenticateAction {

	@Override
	BaseJsonObject<LoginUser> getLoginUser(String login) {
		AutoLoginPro autoLoginPro = SpringBeanUtils.getBean(AutoLoginPro.class);
		String uids=new String(Base64.decodeBase64(login));
		String splitStr = autoLoginPro.getSplitStr();
		String[] arrayOfString = uids.split((splitStr == null || splitStr.equals(""))? "#" : splitStr);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String userName = "";
		if ((arrayOfString.length > 1) && (arrayOfString[1].equals(sdf.format(new Date())))){
			userName = arrayOfString[0];
		} else {
			return null;
		}
		UserFeignClient userFeignClient = SpringBeanUtils.getBean(UserFeignClient.class);
		BaseJsonObject<LoginUser> baseJsonObject = userFeignClient.getLoginUserByUserName(userName);
		BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        if(baseJsonObject.getData() != null){
        	baseJsonObject.getData().setPasswd(encoder.encode(DigestUtils.md5Hex(login)));
        }
		return baseJsonObject;
	}
}
